require "game/ui/form/dungeon/special/UIBookshelf"

-- 按钮点击回调
function btnOnClick(gridData, index)
    DungeonActionM.go("bookshelf", gridData:getPos(), index);
    UIMgr.getCurrentScene():removeFormByName("UIBookshelf");
end

-- 按钮能否点击
function btnCanClick(gridData, index)
    local bonus = gridData.bonus;

    if bonus[index] then
        local options = gridData.options or {};

        -- 判断是否已经领取
        if table.indexOf(options, index) > 0 then
            return false;
        end

        return true;
    else
        return false;
    end
end

-- 废弃的书架
function initBookshelf(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("ui_open1");

            -- 打开废弃的书架
            local uiForm = UIBookshelf.create(self.gridData, nil, nil, nil, nil, btnCanClick, btnOnClick,
                { getLocStr("book_1"), getLocStr("book_2"), getLocStr("book_3"), getLocStr("book_4"), getLocStr("book_5"), getLocStr("book_6"),});
            UIMgr.getCurrentScene():removeFormByName("UIBookshelf");
            UIMgr.getCurrentScene():addForm(uiForm);
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
end
